﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESHOP.DAO;
using E_Shop.Common;
using ESHOP.Domain;
using ESHOP.UTIL;
namespace E_Shop
{
    public partial class TradeLeadList :Authentication
    {
        private readonly SanPhamDAO _sanphamdao = new SanPhamDAO();
        private readonly SellLeadDao _selldao = new SellLeadDao();
        private readonly BuyLeadDAO _buydao = new BuyLeadDAO();
        private readonly TheyeucauDAO _theyeucaudao = new TheyeucauDAO();
        private readonly HopthuDAO _emaildao = new HopthuDAO();
        private bool flag;

        public bool Flag
        {
            get { return flag; }
            set { flag = value; }
        }
        String id = ""; String keyword = ""; String counttry = ""; String category = "";
        protected void Page_Load(object sender, EventArgs e)
        {
          
            BindDataForDatalist();
            BindDataForBuing();
        }

        private void BindDataForBuing()
        {
            id = (String)Request.QueryString["id"];
            keyword = (String)Request.QueryString["textsearch"];
            counttry = (String)Request.QueryString["idcounttry"];
            category = (String)Request.QueryString["idcategory"];
            var objlist = _buydao.GetFromCategory(id);

            CollectionPager2.MaxPages = 10000;
            CollectionPager2.PageSize = 10; // số items hiển thị trên một trang
            CollectionPager2.DataSource = objlist;
            CollectionPager2.BindToControl = dtlbuying;
            dtlbuying.DataSource = CollectionPager2.DataSourcePaged;
            dtlbuying.DataBind();
            Upbuying.Update();
        }
        public void BindDataForDatalist()
        {
            id = (String)Request.QueryString["id"];
            keyword = (String)Request.QueryString["textsearch"];
            counttry = (String)Request.QueryString["idcounttry"];
            category = (String)Request.QueryString["idcategory"];
            var objlist = _sanphamdao.SearchListProducts(id,keyword, category, counttry);

            CollectionPager1.MaxPages = 10000;
            CollectionPager1.PageSize = 10; // số items hiển thị trên một trang
            CollectionPager1.DataSource = objlist;
            CollectionPager1.BindToControl = DtlsSelling;
            DtlsSelling.DataSource = CollectionPager1.DataSourcePaged;
            DtlsSelling.DataBind();
            updtlist.Update();
        }
        public void lbtn_addtocart_sell(object sender, EventArgs e)
        {
            Session[Constants.SESSION_URLLATE] = Request.Url;
            if (Acount == null)
            {
                Response.Redirect(WebUrl.LOGIN, true);
            }
            var button = (LinkButton)sender;
            var id = button.CommandArgument.ToString();
            THEYEUCAU _theyeucau = new THEYEUCAU();
            var exitsselling = _theyeucaudao.GetListForCompany_Selling(Acount.MACT);
            var checkselling = exitsselling.Where(p => p.SELLING.Equals(id)).ToList();

            if (checkselling.Count>0)
            {
                ShowWarning_sitermaster("this require cart Selling Exists .");
                return;
            }
            _theyeucau.ID = _theyeucaudao.NewId();

            _theyeucau.MACT = Acount.MACT;
            _theyeucau.SELLING = id.ToString();
            _theyeucau.NGAYADD = DateTime.Now;
            _theyeucau.ISSHOW = true;
            _theyeucau.LOAI = 1;
            if (_theyeucaudao.Insert(_theyeucau) == 1)
            {
                ShowInfor_sitermaster("Add require cart Selling success.");
            }
            else
            {
                ShowError_sitermaster("Add require cart Selling Fail.");
            }
        }
        public void lbtn_contactnow_sell(object sender, EventArgs e)
        {
            Session[Constants.SESSION_URLLATE] = Request.Url;
            if (Acount == null)
            {
                Response.Redirect(WebUrl.LOGIN, true);
            }
            Session[Constants.SESSION_TYPE] = 1;
            var button = (LinkButton)sender;
            var index = button.CommandArgument.ToString();
            var product = _selldao.Get(index);
            Session[Constants.SESSION_PRODUCT] = product;
            UnblockDialog_sitermaster("replyemail");
            lbreceiver.Text = product.CONGTY.TENCONGTY.ToString();
            lbngaygui.Text = DateTime.Now.ToString();
            Upreplyemail.Update();
        }
        public void lbtn_contactnow_buy(object sender, EventArgs e)
        {
            Session[Constants.SESSION_URLLATE] = Request.Url;
            if (Acount == null)
            {
                Response.Redirect(WebUrl.LOGIN, true);
            }
            Session[Constants.SESSION_TYPE] = 2;
            var button = (LinkButton)sender;
            var index = button.CommandArgument.ToString();
            var product = _buydao.Get(index);
            Session[Constants.SESSION_PRODUCT] = product;
            UnblockDialog_sitermaster("replyemail");
            lbreceiver.Text = product.CONGTY.TENCONGTY.ToString();
            lbngaygui.Text = DateTime.Now.ToString();
            Upreplyemail.Update();
        }
        public void lbtn_addtocart_buy(object sender, EventArgs e)
        {

            
            Session[Constants.SESSION_URLLATE] = Request.Url;
            if (Acount == null)
            {
                Response.Redirect(WebUrl.LOGIN, true);
            }
            var button = (LinkButton)sender;
            var id = button.CommandArgument.ToString();
            THEYEUCAU _theyeucau = new THEYEUCAU();
            var exitbuying = _theyeucaudao.GetListForCompany_Buying(Acount.MACT);
           var  checkselling = exitbuying.Where(p => p.BUYING.Equals(id)).ToList();

           if (checkselling.Count > 0)
            {
                ShowWarning_sitermaster("this require cart Selling Exists .");
                return;
            }
            _theyeucau.ID = _theyeucaudao.NewId();
            _theyeucau.MACT = Acount.MACT;
            _theyeucau.BUYING = id.ToString();
            _theyeucau.NGAYADD = DateTime.Now;
            _theyeucau.ISSHOW = true;
            _theyeucau.LOAI =2;
            if (_theyeucaudao.Insert(_theyeucau) == 1)
            {
                ShowInfor_sitermaster("Add require cart Buying success.");
            }
            else
            {
                ShowError_sitermaster("Add require cart Buying Fail.");
            }
        }

        public void btnsend_Click(object sender, EventArgs e)
        {
            HideDialog_sitermaster("replyemail");
             HOPTHU _emmail = new HOPTHU();
             int TYPE = (int)Session[Constants.SESSION_TYPE] ;
             if (TYPE == 1)
            {
                var product = (SELL)Session[Constants.SESSION_PRODUCT];
                _emmail.ID = _emaildao.NewId();
                _emmail.SUBJECT = String.IsNullOrEmpty(txtSubject.Text.Trim()) ? "" : txtSubject.Text.Trim();
                _emmail.CONTENT_MSG = txtcontent.Text.Trim() == null ? "" : txtcontent.Text.Trim();
                _emmail.SENDER = Acount.MACT;
                _emmail.RECEIVER = product.MACONGTY.ToString();
                _emmail.IsRead = false;
                _emmail.ISSHOW = true;
                _emmail.NGAYGUI = DateTime.Now;
            }
            else
            {
                var product = (BUY)Session[Constants.SESSION_PRODUCT];
                _emmail.ID = _emaildao.NewId();
                _emmail.SUBJECT = String.IsNullOrEmpty(txtSubject.Text.Trim()) ? "" : txtSubject.Text.Trim();
                _emmail.CONTENT_MSG = txtcontent.Text.Trim() == null ? "" : txtcontent.Text.Trim();
                _emmail.SENDER = Acount.MACT;
                _emmail.RECEIVER = product.MACONGTY.ToString();
                _emmail.IsRead = false;
                _emmail.ISSHOW = true;
                _emmail.NGAYGUI = DateTime.Now;
            }
           
          
            int kq = _emaildao.Insert(_emmail);
            if (kq == 1)
            {
                ShowInfor_sitermaster("Send email success.");
            }

            else
            {
                ShowError_sitermaster("Send email fail!.");
            }
            Session[Constants.SESSION_PRODUCT] = null;
        }
       
    }
}